package com.moleq.mgpos.widget;

import com.moleq.mgpos.activities.R;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class SaleListItemViewCache {
	private View baseView;  
    
    private TextView textViewEntryNum;
    private TextView textViewName;
    private TextView textViewPrice;
    private TextView textViewQty;
    private TextView textViewWeight;
    private ImageButton btnDelete;
    private LinearLayout lyControls;

	private int mIndex = -1;

	private SaleListItemControlPanel panel;

	private LinearLayout container;

	private LayoutInflater mInflater;

    public SaleListItemViewCache(View baseView) {  
        this.baseView = baseView;  
        
        
//        View view = baseView.findViewById(R.id.linearLayout1);
        panel= new SaleListItemControlPanel(baseView.getContext(),baseView,50,LayoutParams.FILL_PARENT);
        
//        mInflater = (LayoutInflater) baseView.getContext()
//				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        View controlsView = mInflater.inflate(R.layout.sale_list_item_controls, null);
        
//        int width = 0;
//        LinearLayout  controlsView = (LinearLayout) view2.findViewById(R.id.layoutSaleListItemControls);
//        LayoutParams otherLP=(LayoutParams) view2.getLayoutParams();
//        width = otherLP.width;
    
//      //新建测试组件
//      		TextView tvTest=new TextView(baseView.getContext());
//      		tvTest.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
//      		tvTest.setText("测试组件，红字白底");
//      		tvTest.setTextColor(Color.RED);
//      		tvTest.setBackgroundColor(Color.WHITE);
      		//加入到Panel里面
//      		panel.fillPanelContainer(controlsView);
      		container=(LinearLayout)baseView.findViewById(R.id.layoutSaleListItem);
      		container.addView(panel);//加入Panel控件
    }  

    public TextView getTextViewEntryNum() {  
        if (textViewEntryNum == null) {  
        	textViewEntryNum = (TextView) baseView.findViewById(R.id.list_item_num);  
        }  
        return textViewEntryNum;  
    }  
    
    public TextView getTextViewName() {  
        if (textViewName == null) {  
        	textViewName = (TextView) baseView.findViewById(R.id.list_item_name);  
        }  
        return textViewName;  
    }  
    
    public TextView getTextViewDesc() {  
        if (textViewName == null) {  
        	
//        	textViewName = (TextView) baseView.findViewById(R.id.list_item_name2);  
        }  
        return textViewName;  
    } 
    
    public TextView getTextViewPrice(){
    	 if (textViewPrice == null) {  
    		 textViewPrice = (TextView) baseView.findViewById(R.id.list_item_price);  
         }  
         return textViewPrice; 
    }
    
    public TextView getTextViewQty(){
    	 if (textViewQty == null) {  
    		 textViewQty = (TextView) baseView.findViewById(R.id.list_item_qty);  
         }  
         return textViewQty; 
    }
    
//    public TextView getTextViewWeight(){
//    	if (textViewWeight == null) {  
//    		textViewWeight = (TextView) baseView.findViewById(R.id.list_item_weight);  
//        }  
//        return textViewWeight; 
//    }
    
    public ImageButton getButtonDelete(){
    	if (btnDelete == null) {  
    		btnDelete = (ImageButton) baseView.findViewById(R.id.buttonSaleItemDelete);  
        }  
        return btnDelete;
    }
    
//    public LinearLayout getLinearLayoutControls(){
//    	if (lyControls == null) {  
//    		lyControls = (LinearLayout) baseView.findViewById(R.id.ly_sale_list_item_controls);  
//        }  
//        return lyControls;
//    }
    

	public void setDisplayControls(boolean b) {
//		LinearLayout ll = getLinearLayoutControls();
//		LayoutParams lp = (LayoutParams) ll.getLayoutParams();
//		int totalWidth = 0;
//		if (b) {
//			for (int i = 0; i < ll.getChildCount(); i++) {
//				totalWidth += ll.getChildAt(i).getLayoutParams().width;
//			}
//			lp.width = totalWidth;
//		} else {
//			lp.width = 0;
//		}
//		baseView.refreshDrawableState();
		
		if(panel != null){
			if(b){
				panel.OpenPanel();
			}else{
				panel.ClosePanel();
			}
		}

	}

	public void setSelected(boolean b) {
		if(b){
			baseView.setBackgroundColor(Color.rgb(204, 102, 51));
			getTextViewEntryNum().setTextColor(Color.WHITE);
			getTextViewQty().setTextColor(Color.WHITE);
			getTextViewName().setTextColor(Color.WHITE);
//			getTextViewWeight().setTextColor(Color.WHITE);
			getTextViewPrice().setTextColor(Color.WHITE);
		}else{
			baseView.setBackgroundColor(Color.rgb(242, 228, 217));
			getTextViewEntryNum().setTextColor(Color.BLACK);
			getTextViewQty().setTextColor(Color.BLACK);
			getTextViewName().setTextColor(Color.BLACK);
//			getTextViewWeight().setTextColor(Color.BLACK);
			getTextViewPrice().setTextColor(Color.BLACK);
		}
	
		
	}

	public void setPosition(int position) {
		mIndex  = position;
		
	}
	
	public int getPosition() {
		return mIndex ;
		
	}
   
}
